import React ,{Component} from 'react';
import {
    View,
    Text,
} from 'react-native';
//自定义组件
import Login from '../userCenter/login';
import userIndex from './userIndex';
import MyStroage from '../common/myStroage';
import Header from '../common/header';
import * as URLConstant from '../constant/URLconstant'
import Util from '../common/util'
import Toast from '../common/ToastUtil'
export default class loginUser extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.successCallback = this.successCallback.bind(this);
        this.onLoginSuccess = this.onLoginSuccess.bind(this);
        this.state = {
            datas: [],
            isLogin: false,
            loaded: false,
            titleType: 1,
        };
    }

    async componentDidMount() {
        try {
            let that = this;
            let userInfoDate = await MyStroage.getStorageAllDataForKey('userinfo')
            LOG('KEY11111======' + JSON.stringify(userInfoDate));
            if (userInfoDate == null) {
                that.setState({isLogin: false, loaded: false});
                return;
            } else {
                let key = userInfoDate.key;
                Toast.show('登录了')
                //that.onRequestData(key);

            }

        } catch (err) {
            LOG(err);
        }

    }

    async componentWillReceiveProps(nextProps) {
        try {
            let that = this;
            let userInfoDate = await MyStroage.getStorageAllDataForKey('userinfo')
            LOG('KEY11111======' + JSON.stringify(userInfoDate));
            if (userInfoDate == null || userInfoDate == '') {
                that.setState({isLogin: false, loaded: false});
            } else {
                let key
                key = userInfoDate.key;
                // that.onRequestData(key);

            }
        } catch (err) {
            LOG(err);
        }
    }

    //登录成功
    onLoginSuccess(key) {
        // this.onRequestData(key);
        Toast.show('登录成功')
        this.setState({
            loaded: true,
        })
    }

    async onRequestData(key) {
        try {
            LOG("登录成功的key=====" + key);
            let datakey = [];
            if(key!=null||key!=''){
                datakey['key'] = key;
                let MESSAGE_URL = URLConstant.MESSAGE_URL;
                await Util.get(MESSAGE_URL, datakey, this.successCallback, this.errorcallback);
                this.setState({ key1: key});
            }

        } catch (e) {

        }
    }

    successCallback(datas) {
        let data = datas['data']

        this.setState({
            datas: data,
            isLogin: true,
            loaded: false
        })
    }

    errorcallback(err) {
        LOG('errorcallback==' + err);
    }

    render() {
        let datas = this.state.datas;
        let key1 = this.state.key1;
        return (
            <View style={{flex:1}}>
                {this.state.loaded ?
                    <View style={{flex:1}}>
                        {Util.loading()}
                    </View>
                    :
                    <View style={{flex:1}}>
                        {this.state.isLogin ?
                            <View style={{flex:1}}>
                                <Header navigator={this.props.navigator}
                                        initObj={{ title: '消息',id:this.state.titleType}}/>
                                <userIndex navigator={this.props.navigator}/>
                            </View>
                            :
                            <Login navigator={this.props.navigator}
                                   onLoginSuccess={this.onLoginSuccess}/>
                        }
                    </View>
                }
            </View>
        )
    }
}
